import json
from tkinter.messagebox import showwarning
from bases import match_method_dict
from bases.cases.add_report_data import add_report_data
from bases.cases.mes_upload import MES_upload
from bases.cases.scan_sn import case_scan_sn
from bases.enum_match_method import MatchMethod
from bases.atest_case import ATestCase
from bases.atest_case_all import ATestCaseAll
import time
import os.path
from cases import titles, cases_id, limits_H, limits_L, methods, main_cases, main_cases_length
from commons import case_func_dict, main_testcase_2, main_testcase_1
from commons.public_gobals import PublicGlobals
from test_py.BL135_function import open_ser, DUT_turn_on, DUT_to_Testing, close_ser, DUT_turn_off, get_BT_version, \
    get_MCU_version, get_BTNAME, get_BTMAC, LED_ON_W_Test, LED_ON_R_Test, LED_ON_G_Test, LED_ON_B_Test, get_bat_temp, \
    get_bat_voltage, get_bat_level, get_key_in_Test, get_key_out_Test, get_key_power, get_key_light, get_key_Auxxxx, \
    get_Auracast_conn, DUT_RST

count = 0
def case_return_3():
    global count
    count+=1
    # print(f'执行。。。{count}')
    time.sleep(1.2)
    return 3

class CasesConfig:
    all_cases:ATestCaseAll = ATestCaseAll()

    file_ini_path = './datafiles/main_limit_args.json'
    file_ini_dir = './datafiles'

    @classmethod
    def case_init(cls):
        # 创建用例--init_case
        init_case_base1 = ATestCase(limit_H=1, match_method=MatchMethod.eq, fresh_loop=True, loop=5, case_func=lambda: 1,
                                    title='init测试项1')
        init_case_base2 = ATestCase(limit_H=3, match_method=MatchMethod.eq, fresh_loop=True, loop=1,
                                    case_func=lambda: 2,
                                    title='init测试项1')


        cls.all_cases.add_case(init_case_base1, flag='init')
        cls.all_cases.add_case(init_case_base1, flag='init')

        cls.all_cases.add_case(init_case_base1, flag='pre_main')

        main_case_base1 = ATestCase(limit_H=1, match_method=MatchMethod.eq, fresh_loop=True, loop=5,
                                    case_func=main_testcase_1,
                                    title='main测试项1')
        main_case_base2 = ATestCase(limit_H='PublicGlobals.sn_Seq', match_method=MatchMethod.eq, fresh_loop=True, loop=1,
                                    case_func=main_testcase_2,
                                    enable='PublicGlobals.bool_temp_0',
                                    title='main测试项2')
        cls.all_cases.add_case(main_case_base1, flag='main')
        cls.all_cases.add_case(main_case_base2, flag='main')

        cls.all_cases.add_case(init_case_base1, flag='post_main')
        cls.all_cases.add_case(init_case_base1, flag='clear')


        # pre_main
        pre_case_1 = ATestCase(limit_H=str(True), match_method=MatchMethod.eq,
                               fresh_loop=True, loop=5, case_func=open_ser,fail_time=2,
                                    title='打开串口')
        pre_case_2 = ATestCase(limit_H=str(True), match_method=MatchMethod.eq, fresh_loop=True, loop=5,
                               case_func=DUT_to_Testing,
                                    title='进入产测模式')
        pre_case_3 = ATestCase(limit_H=str(True), match_method=MatchMethod.eq, fresh_loop=True, loop=5,
                               case_func=DUT_turn_on,
                               title='开机')
        # cls.all_cases.add_case(pre_case_1, flag='pre_main')
        # cls.all_cases.add_case(pre_case_2, flag='pre_main')
        # cls.all_cases.add_case(pre_case_3, flag='pre_main')

        # SN
        scan_sn = ATestCase(limit_H="True", match_method=MatchMethod.eq,loop=1, case_func=case_scan_sn,
                                    title='输入SN')
        cls.all_cases.add_case(scan_sn, flag='scan_sn')

        # 创建用例--main
        main_1 = ATestCase(limit_H='6.0', match_method=MatchMethod.eq, fresh_loop=True, loop=5,
                               case_func=get_BT_version,
                               title='读取BT版本')
        main_2 = ATestCase(limit_H='02', match_method=MatchMethod.eq, fresh_loop=True, loop=5,
                           case_func=get_MCU_version,
                           title='读取MCU版本')
        main_3 = ATestCase(limit_H='HISENSE PARTY RAY', match_method=MatchMethod.eq, fresh_loop=True, loop=5,
                           case_func=get_BTNAME,
                           title='读取BT名称')
        main_4 = ATestCase(limit_H='00:b5:e9:31:3f:da', match_method=MatchMethod.no, fresh_loop=True, loop=5,
                           case_func=get_BTMAC,
                           title='读取BT MAC地址')
        main_5 = ATestCase(limit_H=str(True), match_method=MatchMethod.eq, fresh_loop=True, loop=5,
                           case_func=LED_ON_W_Test,
                           # enable=False,
                           title='灯光测试-白')
        main_6 = ATestCase(limit_H=str(True), match_method=MatchMethod.eq, fresh_loop=True, loop=5,
                           case_func=LED_ON_R_Test,
                           title='灯光测试-红')
        main_7 = ATestCase(limit_H=str(True), match_method=MatchMethod.eq, fresh_loop=True, loop=5,
                           case_func=LED_ON_G_Test,
                           title='灯光测试-绿')
        main_8 = ATestCase(limit_H=str(True), match_method=MatchMethod.eq, fresh_loop=True, loop=5,
                           case_func=LED_ON_B_Test,
                           title='灯光测试-蓝')
        main_9 = ATestCase(limit_H=30, match_method=MatchMethod.between, fresh_loop=True, loop=5,
                           limit_L=20,
                           case_func=get_bat_temp,
                           title='读取电池温度')
        main_10 = ATestCase(limit_H=4.0, match_method=MatchMethod.between, fresh_loop=True, loop=5,
                           limit_L=3.5,
                           case_func=get_bat_voltage,
                           title='读取电池电压')
        main_11 = ATestCase(limit_H=80, match_method=MatchMethod.between, fresh_loop=True, loop=5,
                           limit_L=70,
                           case_func=get_bat_level,
                           title='读取电池电量',
                           modify_flag=True,
                           modify=-5 )
        main_12 = ATestCase(limit_H=str(True), match_method=MatchMethod.eq, fresh_loop=True, loop=5,
                           case_func=get_key_in_Test,
                           title='进入按键测试模式')

        main_13 = ATestCase(limit_H=str(True), match_method=MatchMethod.eq, fresh_loop=True, loop=5,
                           case_func=get_key_power,
                           title='按键测试-Power')
        main_14 = ATestCase(limit_H=str(True), match_method=MatchMethod.eq, fresh_loop=True, loop=5,
                           case_func=get_key_light,
                           title='按键测试-Light')
        main_15 = ATestCase(limit_H=str(True), match_method=MatchMethod.eq, fresh_loop=True, loop=5,
                            case_func=get_key_Auxxxx,
                            title='按键测试-Auracast')
        main_16 = ATestCase(limit_H=str(True), match_method=MatchMethod.eq, fresh_loop=True, loop=5,
                            case_func=get_key_out_Test,
                            title='退出按键测试模式')

        main_17 = ATestCase(limit_H=str(True), match_method=MatchMethod.eq, fresh_loop=True, loop=5,
                            case_func=get_Auracast_conn,
                            title='Auracast连接测试')

        main_18 = ATestCase(limit_H=str(True), match_method=MatchMethod.eq, fresh_loop=True, loop=5,
                            case_func=DUT_RST,
                            title='复位')
        # 添加用例
        # cls.all_cases.add_case(main_1)
        # cls.all_cases.add_case(main_2)
        # cls.all_cases.add_case(main_3)
        # cls.all_cases.add_case(main_4)
        # # 灯光测试
        # cls.all_cases.add_case(main_5)
        # cls.all_cases.add_case(main_6)
        # cls.all_cases.add_case(main_7)
        # cls.all_cases.add_case(main_8)
        # cls.all_cases.add_case(main_9)
        # cls.all_cases.add_case(main_10)
        # cls.all_cases.add_case(main_11)
        # cls.all_cases.add_case(main_12)
        # cls.all_cases.add_case(main_13)
        # cls.all_cases.add_case(main_14)
        # cls.all_cases.add_case(main_15)       # Auracast按键
        # cls.all_cases.add_case(main_16)
        # cls.all_cases.add_case(main_17)
        # cls.all_cases.add_case(main_18)       # 复位



        # cls.all_cases.add_case(init_case_base1)



        # post_main
        # post_case_1 = ATestCase(limit_H=str(True), match_method=MatchMethod.eq, fresh_loop=True, loop=5,
        #                        case_func=DUT_turn_off,
        #                        title='产品关机')
        # cls.all_cases.add_case(post_case_1, flag='post_main')
        # post_case_1 = ATestCase(limit_H=str(True), match_method=MatchMethod.eq, fresh_loop=True, loop=5,
        #                        case_func=close_ser,
        #                        title='关闭串口')
        # cls.all_cases.add_case(post_case_1, flag='post_main')

        ## 生成报表，固定在这里
        report_add_case = ATestCase(limit_H="True", match_method=MatchMethod.eq,loop=1, case_func=add_report_data,
                                    title='本地记录测试数据')
        cls.all_cases.add_case(report_add_case, flag='post_main')
        mes_upload_case = ATestCase(limit_H="True", match_method=MatchMethod.eq,loop=1, case_func=MES_upload,
                                    enable=PublicGlobals.MES_Enable,
                                    title='MES过站数据上传')
        cls.all_cases.add_case(mes_upload_case, flag='post_main')


        # 创建用例--clear_case
        clear_case_base1 = ATestCase(limit_H=3, match_method=MatchMethod.eq, fresh_loop=True, loop=5, case_func=lambda: 3,
                                    title='clear测试项1')
        # cls.all_cases.add_case(clear_case_base1, flag='clear')
        # cls.all_cases.add_case(clear_case_base1, flag='clear')



        # cls.all_cases.add_case(case_base2, flag='scan_sn')    # 错误
        # cls.all_cases.add_case(init_case_base1, flag='scan_sn')   # 正确



        # print(cls.all_cases.pre_main_case_list)


        cls.get_main_cases_info()           # 将用例的一些信息传递出去----原初信息的传递
        cls.load_limit_args()               # 给用例加载文件中的参数----


    @classmethod
    def case_run(cls):
        cls.all_cases.run()

    @classmethod
    def get_main_cases_info(cls):
        # global titles,cases_id,limits_H,limits_L,methods
        main_cases.extend(cls.all_cases.get_main_cases())
        main_cases_length[0] = len(main_cases)
        print('测试。。。。。。。。。。。。。。。',main_cases)
        num = 1
        # titles = []
        # cases_id = []
        # limits_H = []
        # methods = []
        # limits_L = []
        for main_case in main_cases:
            titles.append(main_case.title)       # 测试项标题
            cases_id.append('main_'+str(num).rjust(2,'0'))   # 测试项编号
            limits_H.append(main_case.limit_H)           # 测试上限
            methods.append(' '+main_case.match_method.value) # 测试方法
            limits_L.append(main_case.limit_L)           # 测试下限
            num += 1
        print(methods)
        # return titles,cases_id,limits_H,methods,limits_L

    @classmethod
    def args_file_is_exist(cls) -> bool:
        return os.path.exists(cls.file_ini_path)

    @classmethod
    def load_limit_args(cls):  # 加载limit_args里的数据
        if not cls.args_file_is_exist():
            print('文件不存在，不加载')
            return
        try:
            args_list:list|None = None
            # case_func_dict
            with open(cls.file_ini_path, 'r', encoding='utf-8') as f:
                args_list = json.load(f)
                for args, case in zip(args_list, cls.all_cases.get_main_cases()):
                    case.title = args['title']
                    case.limit_H = args['limit_H']
                    case.limit_L = args['limit_L']
                    case.match_method = match_method_dict[args['match_method']]
                    case.enable = args['enable']
                    case.loop = args['loop']
                    case.fresh_loop = args['fresh_loop']
                    case.force_loop = args['force_loop']
                    case.pre_time = args['pre_time']
                    case.post_time = args['post_time']
                    case.fail_time = args['fail_time']
                    case.modify_flag = args['modify_flag']
                    case.modify = args['modify']
                    case.case_func = case_func_dict[args['case_func']]      # 实际保存为默认值，更新需要根据实际加载
                    case.func_args_info = args['func_input_args']           # 刷新函数信息
                    case.limit_args_2_global_value()    # 更新run字段的数据
                    case.update_func_args_info()     # 更新函数参数信息
                    # tmp = args['case_func']
                    # print(case_func_dict[tmp])
            print('加载limit参数数据成功')
        except Exception as e:
            print('加载limit参数数据失败，原因为', e)
            showwarning(message='加载参数失败')

